package com.kitty.game.fight.model;

import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.Pair;
import com.kitty.game.fight.SkillConst;
import com.kitty.game.fight.artifact.active.ArtifactSkillProcessor;
import com.kitty.game.fight.artifact.passive.ArtifactType;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.book.BookSkillProcessor;
import com.kitty.game.fight.hunqi.passive.HunQiType;
import com.kitty.game.fight.message.RespAttackSkillLightEffect;
import com.kitty.game.fight.pet.PetSkillProcessor;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.fight.util.FightRateUtil;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.skill.model.RoleSkill;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.StatusConst;
import com.kitty.mina.message.Message;
import org.nutz.lang.util.NutMap;

import java.util.List;
import java.util.Random;

import static com.kitty.game.utils.StatusConst.STATUS_PASSIVE_ATTACK;
import static com.kitty.game.utils.StatusConst.STATUS_PASSIVE_MAG_ATTACK;
public class BattleUnit {

    /**
     * 出手方
     */
    private FightObject effector;
    /**
     * 目标
     */
    private FightObject target;
    /**
     * 伤害值
     */
    private int hurt;

    private RoleSkill skill;

    private int skillType;

    private Fight fight;

    /**
     * 是否是反击
     */
    private boolean isBackAttack;
    /**
     * 是否必杀
     */
    private boolean isStunt;
    /**
     * 是否破防
     */
    private boolean isPenetrate;
    /**
     * 是木系辅助
     */
    private boolean isWoodAssist = false;
    /**
     * 是木系障碍
     */
    private boolean isWoodObstacle = false;
    /**
     * 目标是否躲避
     */
    private boolean isDodge;
    /**
     * 目标是否反震
     */
    private boolean isDamage;
    private int skillLevel;

    private int doubleHit = 1;

    /**
     * 技能反伤
     */
    private int backHurt = 0;

    /**
     * 神龙罩
     */
    private boolean immunePhyDamage;

    /**
     * 乾坤罩
     */
    private boolean passiveAttack;

    /**
     * 天书
     */
    private int bookId;

    /**
     * 触发混元金斗法宝
     */
    private boolean triggerDipper = false;

    /**
     * 阴阳镜法宝转移的目标
     */
    private FightObject mirrorTarget;

    /**
     * 技能的所有命中目标
     */
    private List<Integer> skillTargets;

    public BattleUnit(Fight fight, FightObject effector, FightObject target) {
        this(fight, effector, target, null, 0);
    }

    public BattleUnit(Fight fight, FightObject effector, FightObject target, RoleSkill skill, int skillLevel) {
        this.effector = effector;
        this.target = target;
        this.fight = fight;
        this.skill = skill;
        this.skillLevel = skillLevel;
        if (target.getRole() != null) {
            this.skillLevel += target.getRole().getExtraSkillLevel();
        }
        if (skill != null) {
            this.skillType = skill.getType();
        }
        if (target.isPlayer()) {
            this.mirrorTarget = (FightObject) ArtifactType.MIRROR.getEffect().trigger(fight, target);
        }
    }

    public void setHurt(int hurt) {
        this.hurt = hurt;
    }

    public boolean isPenetrate() {
        return isPenetrate;
    }

    public void setPenetrate(boolean penetrate) {
        this.isPenetrate = penetrate;
    }

    public int getHurt() {
        return hurt;
    }

    public void triggerPhysic() {
        Random random = new Random();
        // 计算人物物攻必杀率
        NutMap petExtra = effector.getExtraAttr();
        if (effector.isPlayer()) {
            if ((random.nextInt(100) + 1) <= effector.getRole().getStuntRate()) {
                isStunt = true;
            } else if ((random.nextInt(100) + 1) <= effector.getRole().getDoubleHitRate()) {
                doubleHit = effector.getRole().getDoubleHit();
                if (doubleHit <= 0) {
                    doubleHit = 2;
                }
                if (doubleHit >=9){
                    doubleHit = 9;
                }
            }
            /** 必杀连击只能触发一个 但是破防不限制 */
            if ((random.nextInt(100) + 1) <= effector.getRole().getPenetrateRate()) {// 破防率
                isPenetrate = true;
            }
        } else if (effector.isPet()) {
            if ((random.nextInt(100) + 1) <= petExtra.getInt("stunt")) {
                isStunt = true;
            } else if ((random.nextInt(100) + 1) <= petExtra.getInt("double")) {
                doubleHit = petExtra.getInt("doubleHit");
            }
            // 破天--破防
            if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.PO_TIAN)) {
                isPenetrate = true;
            }
        } else if (effector.isSuperBoss()) {
            isPenetrate = true;
        }

        if (effector.isPlayer() || effector.isSuperBoss()) {
            triggerDipper = (boolean) ArtifactType.DIPPER.getEffect().trigger(fight, effector);
            if (triggerDipper) {
                doubleHit = 0;
                isStunt = false;
            }
        }

        if (isStunt) {
            if (BookSkillProcessor.defenseTrigger(target, fight, BookSkillProcessor.YUN_TI)) {
                // 云体忽略必杀
                isStunt = false;
            }
        }


        CountService countService = SpringUtils.getBean(CountService.class);
        hurt = countService.countHurt(fight, effector, target, isPenetrate);

        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.JING_LEI)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.JING_LEI).effect(fight,
                    effector, target));
            bookId = SpringUtils.getSkillService().getBookSkillId(BookSkillProcessor.JING_LEI);
        }
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.QING_MU)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.QING_MU).effect(fight,
                    effector, target));
            bookId = SpringUtils.getSkillService().getBookSkillId(BookSkillProcessor.QING_MU);
        }
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.HAN_BING)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.HAN_BING).effect(fight,
                    effector, target));
            bookId = SpringUtils.getSkillService().getBookSkillId(BookSkillProcessor.HAN_BING);
        }
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.LIE_YAN)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.LIE_YAN).effect(fight,
                    effector, target));
            bookId = SpringUtils.getSkillService().getBookSkillId(BookSkillProcessor.LIE_YAN);
        }
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.SUI_SHI)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.SUI_SHI).effect(fight,
                    effector, target));
            bookId = SpringUtils.getSkillService().getBookSkillId(BookSkillProcessor.SUI_SHI);
        }

        if (isStunt) {
            hurt = (int) (hurt * 1.5);
        }
        if (hurt > target.getCurrLife()) {
            hurt = target.getCurrLife();
        }
        if (triggerDipper) {
            hurt *= 2.5;
        }

        int tempDodge = target.getExtraDodge();
        if (target.isPlayer()) {
            tempDodge += target.getRole().getDodge();
        }
        isDodge = tempDodge >= random.nextInt(100) + 1;
        /** 如果目标躲避了 伤害置0 */
        if (isDodge) {
            hurt = 0;
        }else{
            // 乾坤罩反弹物理攻击
            Pair<Integer, Integer> qianKun = (Pair<Integer, Integer>) PetSkillProcessor
                    .queryProcessor(SkillConst.SKILL_QIAN_KUN).effect(fight, effector, target, hurt);
            passiveAttack = qianKun.getSecond() > 0;
            if (passiveAttack) {
                hurt = qianKun.getFirst();
                backHurt = qianKun.getSecond();
                //target.clearAssistBuff_STATUS_PASSIVE_ATTACK();
            }
            //计算是否反震
            if(backHurt == 0){
                double damageSel = 0;
                int damageSelRate = 0;
                if (target.isPlayer()) {
                    damageSelRate = target.getRole().getDamageSelRate();
                }
                //未躲避 ,未反伤 计算反震率。
                isDamage = damageSelRate >= random.nextInt(100) + 1;
            }
        }
        afterUsePhysic();

        // 魔引--扣对方蓝
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.MO_YIN)) {
            BookSkillProcessor.queryProcessor(BookSkillProcessor.MO_YIN).effect(fight, effector, target);
        }


    }


    /**
     * 处理反伤
     */
    private void doBackHurt(boolean phyAttack) {
        if (backHurt == 0) {
            return;
        }
        if(target.isFallDown()){
            return;
        }
        FightMessageService fightMessageService = SpringUtils.getFightMessageService();
        effector.setCurrLife(effector.getCurrLife() - backHurt);
        Message message = fightMessageService.decreaseLife(effector.getId(), effector.getId(), -backHurt, 1, 0);
        FightMessageUtil.sendFightPackage(fight, message);
        FightMessageUtil.sendFightPackage(fight,
                fightMessageService.updateTargetLife(effector.getId(), effector.getCurrLife()));

        if (phyAttack) {
            NutMap status = target.getStatusMap();
            if (status.getInt(String.valueOf(STATUS_PASSIVE_ATTACK),0) == 0) {
                fightMessageService.sendFightAction(fight, target);
                //乾坤罩 消失，  不消失
                //FightMessageUtil.sendFightPackage(fight,SpringUtils.getBean(FightMessageService.class).sendFightObjectStatus(target));
            }
            if((isDamage || isBackAttack) && !target.isFallDown()){
                isBackAttack = false;
                if(isDamage){
                    FightMessageUtil.sendFightPackage(fight,new RespAttackSkillLightEffect(target.getId(),(short)0,4,"反震"));
                }
                // 反震触发轮回
                HunQiType.LUNHUI.getEffect().trigger(fight, target);
            }
        }else{
            NutMap status = target.getStatusMap();
            if (status.getInt(String.valueOf(STATUS_PASSIVE_MAG_ATTACK),0) == 0) {
                fightMessageService.sendFightAction(fight, target);
            }
        }
        backHurt = 0;
    }

    private void doPhyHit() {
        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);

        int tempHurt = hurt;

        // 天书--仙风
        boolean bookXianFeng = false;
        if (BookSkillProcessor.defenseTrigger(target, fight, BookSkillProcessor.XIAN_FENG)) {
            bookXianFeng = true;
        }
        Random random = new Random();
        doubleHit = Math.max(doubleHit, 1);
        Boolean flag = true;
        for (int i = 0; i < doubleHit; i++) {
            if (i > 0) {
                tempHurt /= 2;
                hurt = tempHurt;
            }

            if (hurt < 1 && !passiveAttack) {
                hurt = 1;
            }
            int myAction = 1;
            if (isBackAttack) {
                myAction = 2;//反击是站在原地的

                doubleHit = 0;//反击不让连击
            }
            int targetAction = 1;
            if (isDodge) {
                targetAction = 0;
            }else{

            }
            if (target.isFrozen()) { /** 如果目标在冰冻状态 那么目标不能做任何动作 否则会卡客户端 */
                targetAction = 2;
            }
            if(flag && isDamage && !target.isFallDown() && !isBackAttack ){
                double damageSel = target.getRole().getDamageSel();
                backHurt = (int)(hurt * (damageSel/100));
                //myAction += 8;
                flag = false;
            }
            if (isStunt) {/** 如果必杀了 + 32 */
                myAction += 32;
            }
            if (isPenetrate) { /** 如果破防了 + 8192 */
                myAction += 8192;
            }

            if (bookId > 0) {
                myAction += 512;
            }

            if (bookId == 0 && i > 0) {
                myAction = 17;
                if (isPenetrate) { /** 如果破防了 + 8192 */
                    myAction += 8192;
                }
            }

            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.combatAcceptHit(effector, targetAction, myAction, bookId));

            if (bookXianFeng && i > 0) {
                break;
            }



            // 神龙罩免疫物理攻击
            Pair<Boolean, Integer> shenLong = (Pair<Boolean, Integer>) PetSkillProcessor
                    .queryProcessor(SkillConst.SKILL_SHEN_LONG).effect(fight, effector, target, hurt);
            immunePhyDamage = shenLong.getFirst();
            if (immunePhyDamage) {
                hurt = shenLong.getSecond();
            }

            doDamageToTarget(target, hurt, 1);

            doBackHurt(Boolean.TRUE);

            /** 目标倒地 或者我倒地 或者目标躲避 连击终止 */
            if (target.isFallDown() || effector.isFallDown() || isDodge) {
                break;
            }
        }
    }

    public void doDamageToTarget(FightObject target, int hp, int type) {
        /**受攻击为超级boss，同时战斗中还有超级boss阵魄存在，
         * 则超级boss不受到伤害，同时说话：阵魄不死休能伤的本尊*/
        if (target.isSuperBoss() && SpringUtils.getFightService().isHasSuperBossZP(fight)) {
            String shouting = I18nIdDataPool.getI18nContent(I18nId.PMT_1016);
            Message message = SpringUtils.getBean(FightMessageService.class).fightToMsg(target, shouting);
            FightMessageUtil.sendFightPackage(fight, message);
            return;
        }
        if (target.isSuperBoss() && SpringUtils.getFightService().isHasSuperBossZPJS(fight)) {
            if(hp>0){
                hp = (int)(hp * 0.4);
                hurt = (int)(hurt * 0.4);
            }
        }

        // 是否触发定海珠效果(无法免疫木系)
        if (!isWoodObstacle()) {
            boolean pearlEffect = (boolean) ArtifactType.PEARL.getEffect().trigger(fight, target);
            if (pearlEffect) {
                ArtifactType.PEARL.getEffect().broadCast(fight, target);
                return;
            }
        }

        // 法力护盾抵挡伤害
        hp = (int) PetSkillProcessor.queryProcessor(SkillConst.SKILL_FA_LI).effect(fight, effector, target, hp);

        // 移花接木抵挡伤害
        hp = (int) PetSkillProcessor.queryProcessor(SkillConst.SKILL_YI_HUA).effect(fight, effector, target, hp);

        FightObject damageTarget = target;
        if (mirrorTarget != null) {
            damageTarget = mirrorTarget;
        }
        damageTarget.setCurrLife(damageTarget.getCurrLife() - hp);

        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);

        // 阴阳镜转移伤害
        if (mirrorTarget != null) {
            ArtifactType.MIRROR.getEffect().broadCast(fight, target, mirrorTarget);

            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.decreaseLife(effector.getId(), mirrorTarget.getId(), -hp, 0, 0));
            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.updateTargetLife(mirrorTarget.getId(), mirrorTarget.getCurrLife()));

            if (mirrorTarget.isFallDown() && mirrorTarget.getId() != effector.getId()) {
                FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(mirrorTarget.getId(), 2));
                fightMessageService.sendFightAction(fight, mirrorTarget);
                fight.onActorDie(mirrorTarget);
            }
        } else {
            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.decreaseLife(effector.getId(), target.getId(), -hurt, type, 0));
            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.updateTargetLife(target.getId(), target.getCurrLife()));
        }
        if(hp>0){
            // 受到伤害 触发润泽
            HunQiType.RUNZE.getEffect().trigger(fight, target);
            // 造成伤害 触发灵咒
            HunQiType.LINGZHOU.getEffect().trigger(fight, effector,target);
        }
        // 法宝金刚圈被动效果--反弹伤害
        ArtifactSkillProcessor.getSkillProcessor(ArtifactSkillProcessor.RING_SKILL).delayAction(fight, effector,
                damageTarget, hp);
        // 法宝物极必反被动技能
        ArtifactType.EXTREME.getEffect().trigger(fight, target);
        // 法宝攻击触发天眼
        ArtifactType.SKY_EYE.getEffect().trigger(fight, effector);

    }

    private void afterUsePhysic() {
        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);
        // 出手动作
        FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionStart((short) fight.getRound(), effector));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.updateObjectInfo(effector));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.fightShouting(effector, "物攻"));
        // 混元金斗特效
        if (triggerDipper) {
            ArtifactType.DIPPER.getEffect().broadCast(fight, effector);
        }

        doPhyHit();
    }

    public void triggerMagic() {
        CountService countService = SpringUtils.getHurtService();
        NutMap targetStatus = target.getStatusMap();// 目标状态

        if (effector.isPet()) {
            // 破天--破防
            if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.PO_TIAN)) {
                isPenetrate = true;
            }
        } else if (effector.isSuperBoss()) {
            isPenetrate = true;
        }

        if (skillType == 146) {
            hurt = countService.countHurt(fight, effector, target, isPenetrate);
            if (isStunt) {
                hurt *= 1.5;
            }
            /** 如果躲避了 伤害置0 */
            isDodge = target.popTempCache("dodge", false);
            if (isDodge) {
                hurt = 0;
            }
            if (hurt > target.getCurrLife()) {
                hurt = target.getCurrLife();
            }
        } else if (skillType == 138) {
            if (effector.getPolar() == Const.SCHOOL_METAL) {
                // 阶数*8*施法技能等级
                int attack = skill.getSkillJieshu() * 20 * skillLevel;
                if (!target.isPoison()) {
                    target.setStatus(StatusConst.STATUS_PHY_POWER_UP, 4);
                    target.setStatusScore(StatusConst.STATUS_PHY_POWER_UP, attack);
                    target.setStatus(StatusConst.STATUS_SPEED_UP, 0);
                }
            } else if (effector.getPolar() == Const.SCHOOL_WOOD) {
                isWoodAssist = true;
                target.setStatus(StatusConst.STATUS_RECOVER_LIFE, 4);
                // 阶数*45*施法技能等级
                hurt = skill.getSkillJieshu() * 40 * skillLevel;
                target.setStatusScore(StatusConst.STATUS_RECOVER_LIFE, hurt);
            } else if (effector.getPolar() == Const.SCHOOL_WATER) {

                int defense = skill.getSkillJieshu() * 20 * skillLevel;
                if (!target.isPoison()) {
                    target.setStatus(StatusConst.STATUS_DEF_UP, 4);
                    target.setStatusScore(StatusConst.STATUS_DEF_UP, defense);
                    target.setStatus(StatusConst.STATUS_DODGE_UP, 0);
                }
            } else if (effector.getPolar() == Const.SCHOOL_FIRE) {
                // 阶数*3*施法技能等级
                int speed = skill.getSkillJieshu() * 10 * skillLevel;
                if (!target.isPoison()) {
                    target.setStatus(StatusConst.STATUS_SPEED_UP, 4);
                    target.setStatus(StatusConst.STATUS_PHY_POWER_UP, 0);
                    target.setStatusScore(StatusConst.STATUS_SPEED_UP, speed);
                }
            } else if (effector.getPolar() == Const.SCHOOL_EARTH) {

                int dodge = new Double(skill.getSkillJieshu() * 0.06 * skillLevel).intValue();
                if (!target.isPoison()) {
                    target.setStatus(StatusConst.STATUS_DODGE_UP, 4);
                    target.setStatus(StatusConst.STATUS_DEF_UP, 0);
                    target.setStatusScore(StatusConst.STATUS_DODGE_UP, dodge);
                }
            }
        } else if (skillType == 139) {

        } else if (skillType == 147) {
            int rate = FightRateUtil.getZhangAiRate(fight, effector, target);
            int value = new Random().nextInt(100);
            if (effector.getPolar() == Const.SCHOOL_METAL) {
                if (target.isFrozen()) {
                    rate = 0;
                }
                if (value < rate) {
                    if (target.isConfusion()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_CONFUSION), 0);
                    }
                    if (target.isSleep()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_SLEEP), 0);
                    }
                    if (rate == 50) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FORGOTTEN), 1);
                    } else {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FORGOTTEN), 4);
                    }
                }
            } else if (effector.getPolar() == Const.SCHOOL_WOOD) {
                if (target.isFrozen()) {
                    rate = 0;
                }
                if (value < rate) {
                    isWoodObstacle = true;
                    int forgotten = targetStatus.getInt(String.valueOf(StatusConst.STATUS_FORGOTTEN), 0);
                    int recoverLife = targetStatus.getInt(String.valueOf(StatusConst.STATUS_RECOVER_LIFE), 0);
                    targetStatus.clear();
                    if (forgotten > 0) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FORGOTTEN), forgotten);
                    }
                    if (recoverLife > 0) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_RECOVER_LIFE), recoverLife);
                    }
                    // 阶数*45*施法障碍技能等级
                    hurt = new Double(skill.getSkillJieshu() * 45 * skillLevel).intValue();
                    hurt += hurt * (new Random().nextInt(10) + 1) / 100;
                    hurt = Math.min(target.getCurrLife(), hurt);
                    targetStatus.setv(StatusConst.STATUS_POISON + "value", hurt);
                    if (rate == 50) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_POISON), 1);
                    } else {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_POISON), 4);
                    }
                }
            } else if (effector.getPolar() == Const.SCHOOL_WATER) {
                if (value < rate) {
                    if (target.isConfusion()) {
                        targetStatus.unset(String.valueOf(StatusConst.STATUS_CONFUSION));
                    }
                    if (target.isSleep()) {
                        targetStatus.unset(String.valueOf(StatusConst.STATUS_SLEEP));
                    }
                    if (target.isPoison()) {
                        targetStatus.unset(String.valueOf(StatusConst.STATUS_POISON));
                    }
                    if (target.isForgotten()) {
                        targetStatus.unset(String.valueOf(StatusConst.STATUS_FORGOTTEN));
                    }
                    if (rate == 50) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FROZEN), 1);
                    } else {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FROZEN), 4);
                    }
                }
            } else if (effector.getPolar() == Const.SCHOOL_FIRE) {
                if (target.isFrozen()) {
                    rate = 0;
                }
                if (value < rate) {
                    if (target.isConfusion()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_CONFUSION), 0);
                    }
                    if (target.isPoison()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_POISON), 0);
                    }
                    if (target.isForgotten()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FORGOTTEN), 0);
                    }
                    if (rate == 50) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_SLEEP), 1);
                    } else {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_SLEEP), 4);
                    }
                }
            } else if (effector.getPolar() == Const.SCHOOL_EARTH) {
                if (target.isFrozen()) {
                    rate = 0;
                }
                if (value < rate) {
                    if (target.isPoison()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_POISON), 0);
                    }
                    if (target.isSleep()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_SLEEP), 0);
                    }
                    if (target.isForgotten()) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_FORGOTTEN), 0);
                    }
                    if (rate == 50) {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_CONFUSION), 1);
                    } else {
                        targetStatus.setv(String.valueOf(StatusConst.STATUS_CONFUSION), 4);
                    }
                }
            }
        }

        // 宠物舍命，不能扣血
        if (skill.getSkillId() == SkillConst.SKILL_SHE_MING) {
            hurt = 0;
        }
        // 宠物技能
        PetSkillProcessor petSkillProcessor = PetSkillProcessor.queryProcessor(skill.getSkillId());
        if (petSkillProcessor != null) {
            petSkillProcessor.trigger(fight, effector, target, skillLevel);
        }

        afterUseMagic();
    }

    private void afterUseMagic() {
        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);

        // 如意圈吸收法术伤害
        Pair<Boolean, Integer> ruYiSkill = (Pair<Boolean, Integer>) PetSkillProcessor
                .queryProcessor(SkillConst.SKILL_RU_YI).effect(fight, effector, target, hurt);
        if (ruYiSkill.getFirst()) {
            hurt = ruYiSkill.getSecond();
        }

        // 五色光环反弹法术攻击
        Pair<Integer, Integer> wuSeSkill = (Pair<Integer, Integer>) PetSkillProcessor
                .queryProcessor(SkillConst.SKILL_WU_SE).effect(fight, effector, target, hurt);
        if (wuSeSkill.getSecond() > 0) {
            hurt = wuSeSkill.getFirst();
            backHurt = wuSeSkill.getSecond();
        }

        // 怒击--增强法攻
        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.NU_JI)) {
            hurt = (int) (hurt * (double) BookSkillProcessor.queryProcessor(BookSkillProcessor.NU_JI).effect(fight,
                    effector, target));
        }

        if (skillType == 139) {
            sendStatus();
        } else if (skillType == 147) {// 障碍技能
            FightMessageUtil.sendFightPackage(fight, fightMessageService.checkStatusStart(fight.getRound(), target));
            FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionEnd(target.getId()));
            FightMessageUtil.sendFightPackage(fight, fightMessageService.sendFightObjectStatus(target));
            if (isWoodObstacle()) {
                doMagicHit();
            }
        } else if (skillType == 138) {// 如果是增益技能 需要发送增益包
            sendStatus();
            if (isWoodAssist()) {
                FightMessageUtil.sendFightPackage(fight,
                        fightMessageService.decreaseLife(effector.getId(), target.getId(), getHurt(), 4098, 10005));
                if (target.isFallDown()) {
                    FightMessageUtil.sendFightPackage(fight, fightMessageService.revive(target.getId()));
                }
                target.setCurrLife(target.getCurrLife() + getHurt());
                FightMessageUtil.sendFightPackage(fight,
                        fightMessageService.updateTargetLife(target.getId(), target.getCurrLife()));
            }
        } else if (skillType == 146) {
            doMagicHit();
        }

        doBackHurt(Boolean.FALSE);
    }


    private void doMagicHit() {
        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);

        doDamageToTarget(target, hurt, 4098);
        if (target.isFallDown()) {
            FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(target.getId(), 4098));
            fight.onActorDie(target);
        }
        if (effector.isFallDown()) {
            FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(effector.getId(), 4098));
            fight.onActorDie(effector);
        }
        if (target.isSleep()) {
            /**如果目标是昏睡状态 打一下就给状态清除*/
            target.setStatus(StatusConst.STATUS_SLEEP, 0);
            fightMessageService.sendFightAction(fight, target);
        }

    }

    private void sendStatus() {
        FightMessageService fightMessageService = SpringUtils.getBean(FightMessageService.class);
        FightMessageUtil.sendFightPackage(fight, fightMessageService.checkStatusStart(fight.getRound(), target));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionEnd(target.getId()));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.start(fight.getRound()));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.fightExtra(effector, target));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.end());
        FightMessageUtil.sendFightPackage(fight, fightMessageService.sendFightObjectStatus(target));
    }

    /**
     * 使用力魄技能
     */
    public void afterUseLiPo(Boolean dodge,int targetId,int mainId) {

        FightMessageService fightMessageService = SpringUtils.getFightMessageService();
        // 乾坤罩反弹物理攻击
        if(!dodge){
            Pair<Integer, Integer> qianKun = (Pair<Integer, Integer>) PetSkillProcessor
                    .queryProcessor(SkillConst.SKILL_QIAN_KUN).effect(fight, effector, target, hurt);
            passiveAttack = qianKun.getSecond() > 0;
            if (passiveAttack) {
                hurt = qianKun.getFirst();
                backHurt = qianKun.getSecond();
            }

            // 神龙罩免疫物理攻击
            Pair<Boolean, Integer> shenLong = (Pair<Boolean, Integer>) PetSkillProcessor
                    .queryProcessor(SkillConst.SKILL_SHEN_LONG).effect(fight, effector, target, hurt);
            immunePhyDamage = shenLong.getFirst();
            if (immunePhyDamage) {
                hurt = shenLong.getSecond();
            }
            if(targetId == mainId){
                //计算是否反震
                if(!passiveAttack && !immunePhyDamage){
                    boolean mainTargetDamage = target.getTempCache("damage", false);
                    if(mainTargetDamage){
                        double damageSel = target.getRole().getDamageSel();
                        backHurt = (int)(hurt * (damageSel/100));
                    }
                }
            }
        }
        if (hurt < 1 && !passiveAttack && !immunePhyDamage) {
            hurt = 1;
        }
        doDamageToTarget(target, hurt, 4098);

        doBackHurt(Boolean.TRUE);

//        // 魔引--扣对方蓝
//        if (BookSkillProcessor.attackTrigger(fight, effector, target, BookSkillProcessor.MO_YIN)) {
//            BookSkillProcessor.queryProcessor(BookSkillProcessor.MO_YIN).effect(fight, effector, target);
//        }

        if (target.isFallDown()) {
            FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(target.getId(), 4098));
            fight.onActorDie(target);
        }
        if (effector.isFallDown()) {
            FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(effector.getId(), 4098));
            fight.onActorDie(effector);
        }

        if (target.isSleep()) {
            /**如果目标是昏睡状态 打一下就给状态清除*/
            target.setStatus(StatusConst.STATUS_SLEEP, 0);
            fightMessageService.sendFightAction(fight, target);
        }

    }

    public boolean isWoodAssist() {
        return isWoodAssist;
    }

    public boolean isWoodObstacle() {
        return isWoodObstacle;
    }

    public void setStunt(boolean stunt) {
        this.isStunt = stunt;
    }

    public void setSkillTargets(List<Integer> skillTargets) {
        this.skillTargets = skillTargets;
    }

    public void setBackAttack() {
        isBackAttack = true;
    }
}
